﻿using System.Collections.Generic;
using System.Linq;
using Accountant.DataModel;
using Accountant.Utils;
using Caliburn.Micro;

namespace Accountant.ViewModels
{
  public class EditOperationViewModel : Screen
  {
    public AccountantContext Context { get; set; }
    public TransactionDirection Direction { get; set; }
    public List<Account> PossibleDebit { get; set; }
    public List<Account> PossibleCredit { get; set; }
    public List<Currency> PossibleCurrencies { get; set; }
    public Transaction TransactionUnderConstruction { get; private set; }

    public EditOperationViewModel(AccountantContext context, 
      Transaction transaction, TransactionDirection direction)
    {
      Context = context;
      TransactionUnderConstruction = transaction;
      Direction = direction;
      var acc = Context.Accounts.Where(a => !a.IsHidden && !a.IsAbstract).ToList();
      switch (direction)
      {
        case TransactionDirection.Starting:
          PossibleDebit = acc.Where(a => a.IsSpecial(KnownAccounts.Starting)).ToList();
          PossibleCredit = acc.Where(a => a.IsAsset()).ToList();
          break;
        case TransactionDirection.Income:
          PossibleDebit = acc.Where(a => a.IsIncome()).ToList();
          PossibleCredit = acc.Where(a => a.IsAsset()).ToList();
          break;
        case TransactionDirection.Expense:
          PossibleDebit = acc.Where(a => a.IsAsset()).ToList();
          PossibleCredit = acc.Where(a => a.IsExpense()).ToList();
          break;
        case TransactionDirection.Transfer:
          PossibleDebit = acc.Where(a => a.IsAsset()).ToList();
          PossibleCredit = acc.Where(a => a.IsAsset()).ToList();
          break;
      }
      PossibleCurrencies = Context.Currencies.ToList();
    }
  }
}